﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HT.Data.BussinessLogic;
namespace Hotel.UserControls
{
    public partial class Room : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            try
            {
                DataSet ds = HotelManger.GetInstance().GetAllRoom(0, 0);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gvpayment.DataSource = ds;
                    gvpayment.DataBind();
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("paymentmodeId");
                    dt.Columns.Add("PaymentMode");
                    dt.Columns.Add("SurchargePercent");
                    dt.Columns.Add("Edit");
                    dt.Columns.Add("Delete");
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    gvpayment.DataSource = dt;
                    gvpayment.DataBind();
                    gvpayment.Rows[0].Visible = false;
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void gvpayment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvpayment.EditIndex = e.NewEditIndex;
            LoadData();
        }

        protected void gvpayment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            gvpayment.EditIndex = -1;
            LoadData();
        }

        protected void gvpayment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                if (!string.IsNullOrEmpty(((TextBox)gvpayment.FooterRow.FindControl("txtNameNew")).Text.Trim()))
                {
                    HotelManger.GetInstance().InserteRoom(((TextBox)gvpayment.FooterRow.FindControl("txtNameNew")).Text.Trim(), Convert.ToInt32(((DropDownList)gvpayment.FooterRow.FindControl("ddlRoomType")).SelectedValue),
                        ((CheckBox)gvpayment.FooterRow.FindControl("chkserviceinsert")).Checked, 1);
                    LoadData();
                }
            }

        }

        protected void gvpayment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
            HotelManger.GetInstance().DeleteService(id);
            LoadData();
        }

        protected void gvpayment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Convert.ToInt32(gvpayment.DataKeys[e.RowIndex].Values[0].ToString());
            // string name = ((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtName")).Text;
            // string surcharge = ((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtSurchargePercent")).Text;
            HotelManger.GetInstance().UpdateRoom(id, ((TextBox)gvpayment.Rows[e.RowIndex].FindControl("txtName")).Text.Trim(), Convert.ToInt32(((DropDownList)gvpayment.Rows[e.RowIndex].FindControl("dlRoomType")).SelectedValue),
                     ((CheckBox)gvpayment.Rows[e.RowIndex].FindControl("chkservice")).Checked, 1);
            gvpayment.EditIndex = -1;
            LoadData();
        }

        protected void gvpayment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                ((DropDownList)e.Row.FindControl("ddlRoomType")).DataSource = HotelManger.GetInstance().GetAllRoomType(0, 0);

                ((DropDownList)e.Row.FindControl("ddlRoomType")).DataBind();

            }

            if (e.Row.RowType == DataControlRowType.DataRow && gvpayment.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlRoomType = (DropDownList)e.Row.FindControl("dlRoomType");
                ddlRoomType.DataSource = HotelManger.GetInstance().GetAllRoomType(1, 0);
                ddlRoomType.DataBind();
                ddlRoomType.Items.FindByText((e.Row.FindControl("lRoomType") as Label).Text).Selected = true;
            }
        }


        //protected void dlRoomType_Load(object sender, EventArgs e)
        //{
        //    DropDownList ddl = (DropDownList)sender;
        //    ddl.DataSource = HotelManger.GetInstance().GetAllRoomType(1, 0);
        //    ddl.DataBind();
        //}

        protected void gvpayment_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }


    }
}